package com.insigma.business.qrcode.intell._provider.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.qrcode.common.converter.CareSoldierDemandProviderConverter;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.BizException;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.BaseResponseExtend;
import com.insigma.business.qrcode.common.vo.ProcessStatusEnum;
import com.insigma.business.qrcode.intell._provider.CareSoldierDemandProvider;
import com.insigma.business.qrcode.intell.dto.CareSoldierDemandBSearcher;
import com.insigma.business.qrcode.intell.dto.CareSoldierDemandInsertRequest;
import com.insigma.business.qrcode.intell.entity.CareSoldierDemandDO;
import com.insigma.business.qrcode.intell.service.CareSoldierDemandService;
import com.insigma.business.qrcode.soldier.dto.CareSoldierDemandDTO;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.entity.SoldierBasicInfoDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.CareSoldierDemandSearchRequest;
import com.insigma.business.qrcode.soldier.service.SoldierBasicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Component("careSoldierDemandProvider")
public class CareSoldierDemandProviderImpl extends BaseProviderImpl implements CareSoldierDemandProvider {

    @Autowired
    private SoldierBasicInfoService soldierBasicInfoService;

    @Resource
    CareSoldierDemandService careSoldierDemandService;
    @Override
    public BaseResponse<String> saveCareSoldierDemandToHz(CareSoldierDemandInsertRequest request) {
        BizCheckUtil.checkNotNull(request, "新增CareSoldierDemand的请求参数不能为空");

        String soldierId = request.getSoldierId();

        BizCheckUtil.checkIsTrue(soldierBasicInfoService.checkIfApplyQrSucc(soldierId), "还未获得杭州老兵码！");

        final CareSoldierDemandDO careSoldierDemandDO = CareSoldierDemandProviderConverter.convertRequestIO2DO(request);
        SoldierBasicInfoDO base = soldierBasicInfoService.getByQrSuccess(request.getSoldierId());

        if (Strings.isBlank(request.getTitle())) {
            careSoldierDemandDO.setTitle(request.getType());
        }
        careSoldierDemandDO.setContactNo(base.getMobile());
        careSoldierDemandDO.setClaimantIdCardNo(base.getIdCardNo());
        careSoldierDemandDO.setClaimantName(base.getRealName());
        careSoldierDemandDO.setClaimantAdminAreaCode(base.getResidentAdminAreaCode());
        careSoldierDemandDO.setStatus(ProcessStatusEnum.DEALING.name());
        careSoldierDemandDO.setNeedAssist(false);
        careSoldierDemandService.insert(careSoldierDemandDO);

        return ResponseUtils.buildSuccessResponse(careSoldierDemandDO.getId());
    }

    @Override
    public BasePagingResponse<List<CareSoldierDemandDTO>> searchCareSoldierDemand(CareSoldierDemandSearchRequest searchRequest) {
        final BasePagingResponse<List<CareSoldierDemandDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareSoldierDemand的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CareSoldierDemandBSearcher careSoldierDemandBSearcher = CareSoldierDemandProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(careSoldierDemandBSearcher.getPageNo(), careSoldierDemandBSearcher.getPageSize());
                final List<CareSoldierDemandDO> careSoldierDemandDOList = careSoldierDemandService.search(careSoldierDemandBSearcher);
                PageInfo<CareSoldierDemandDO> pageinfo = new PageInfo<>(careSoldierDemandDOList);
                final List<CareSoldierDemandDTO> careSoldierDemandDTOList = CareSoldierDemandProviderConverter.convertResponseDOList2DTOList(careSoldierDemandDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careSoldierDemandDTOList, searchRequest, pageinfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_SOLDIER_DEMAND_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BaseResponse<String> insertCareSoldierDemand(final CareSoldierDemandInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CareSoldierDemand的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CareSoldierDemandDO careSoldierDemandDO = CareSoldierDemandProviderConverter.convertRequestIO2DO(insertRequest);
                careSoldierDemandService.insert(careSoldierDemandDO);
                ResponseUtils.setupSuccessResponse(baseResponse, careSoldierDemandDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_SOLDIER_DEMAND_INSERT;
            }
        });
        return baseResponse;
    }
    @Override
    public BaseResponse<CareSoldierDemandDTO> getCareSoldierDemand(final BaseGetRequest baseGetRequest) {

        final BaseResponse<CareSoldierDemandDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CareSoldierDemand的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CareSoldierDemandDO careSoldierDemandDO = careSoldierDemandService.get((String) baseGetRequest.getId());
                final CareSoldierDemandDTO remoteObj = CareSoldierDemandProviderConverter.convertResponseDO2DTO(careSoldierDemandDO);










                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_SOLDIER_DEMAND_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> evaluateDemand(CareSoldierDemandInsertRequest request) {

        BizCheckUtil.checkNotNull(request, "请求参数不能为空");
        BizCheckUtil.checkNotNull(request.getId(), "id不能为空");
        BizCheckUtil.checkNotNull(request.getEvaluationStar(), "star不能为空");
        CareSoldierDemandDO careSoldierDemandDO = careSoldierDemandService.get(request.getId());
        if (!(ProcessStatusEnum.SUCCESS.name().equals(careSoldierDemandDO.getStatus())
                || ProcessStatusEnum.REJECT.name().equals(careSoldierDemandDO.getStatus()))) {
            return BaseResponseExtend.error("流程未结束");
//            return null;
        }

        careSoldierDemandDO.setHappenDatetime(new Date());
        careSoldierDemandDO.setEvaluationStar(request.getEvaluationStar());
        careSoldierDemandDO.setEvaluationContent(request.getEvaluationContent());
        careSoldierDemandDO.setModifierId(request.getCreatorId());
        careSoldierDemandService.update(careSoldierDemandDO);

        return BaseResponseExtend.success("评分成功");
    }

}
